package com.example.verification.web;

import com.example.verification.utils.RetBack;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author rcl
 * @date 2019/7/29 10:11
 */
@Controller
public class MainController {
    private static final Logger log = LoggerFactory.getLogger(MainController.class);

    @RequestMapping("/main")
    public String index() {
        log.debug("登录进入主页...............");
        return "index";
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login_v2";
    }

    @PostMapping("/api/login")
    @ResponseBody
    public RetBack apiLogin(String userName, String password, Model model) {

        // 1.获取Subject
        Subject subject = SecurityUtils.getSubject();
        // 2.封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        // 3.执行登录方法
        try {
            subject.login(token);
            return RetBack.<String>builder().data((String) subject.getSession().getId()).code("200")
                    .success(true).errMsg("登录成功").build();
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            model.addAttribute("msg", "用户名不存在！");
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "密码错误！");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return RetBack.<String>builder().data("登录失败").code("200")
                .success(false).errMsg("操作成功").build();
    }

    @PostMapping("/login")
    public String login(String userName, String password, Model model) {

        // 1.获取Subject
        Subject subject = SecurityUtils.getSubject();
        // 2.封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        // 3.执行登录方法
        try {
            subject.login(token);
            return "index";
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            model.addAttribute("msg", "用户名不存在！");
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "密码错误！");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "login_v2";
    }

    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            subject.logout();
        }
        return "redirect:/main";
    }

    @RequestMapping("/error/unAuth")
    public String unAuth() {
        return "/error/unAuth";
    }
}
